package cn.caipengbo.helloandroid;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private EditText editText;
    private Button button1;
    private Button button2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = (EditText) findViewById(R.id.activity1_edit1);
        button1 = (Button) findViewById(R.id.activity1_button1);
        // 使用匿名类
        button1.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                // 页面跳转
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                // 传值
                String msg = editText.getText().toString();
                intent.putExtra("msg", msg);
                // 启动Activity
                startActivity(intent);
            }
        });
    }
    // button2 的事件，普通向下传值
    public void onClickButton2(View view) {
        Intent intent = new Intent(MainActivity.this, Main2Activity.class);
        String msg = editText.getText().toString();
        intent.putExtra("msg", msg);
        // 回调启用Activity需要传递一个整形的 请求码
        startActivityForResult(intent, 2);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // 请求码 和 结果码是用来 区别 相应的
        if (requestCode == 2 && resultCode == 4) {
            String result = data.getStringExtra("result");
            editText.setText(result);
        }

    }

}
